package cn.org.xuanzp.examples.v2.client;

import cn.org.xuanzp.common.command.impl.LoginCommand;
import cn.org.xuanzp.common.utils.CommandUtils;
import cn.org.xuanzp.examples.v2.client.one.MyPayload;
import cn.org.xuanzp.examples.v2.client.one.MyMessageCommand;
import cn.org.xuanzp.nty.client.NettyClient;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @Classname NtyClient
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/9/27 0:08
 * @Created by xuanzp
 */
public class NtyClient {
    String userName = "admin";
    String password = "admin";

    String host = "127.0.0.1";
    int port = 8848;

    private NettyClient nettyClient ;

    public static void main(String[] args) throws Exception {
        NtyClient client = new NtyClient();
        client.connect();
        client.login();
        while (true){
            client.sendMessage();
        }
    }

    private void connect() throws Exception {
        nettyClient = new NettyClient();
        NettyClient.Node node = new NettyClient.Node(host, port);
        nettyClient.addNode(node);
        nettyClient.start();
    }

    private void login() {
        LoginCommand loginCommand  = new LoginCommand();
        loginCommand.setUserName(userName);
        loginCommand.setPassword(password);
        nettyClient.send(host, port, loginCommand);
    }


    private void sendMessage() {

        MyMessageCommand messageCommand = new MyMessageCommand();
        Map<String, Object> header = new HashMap<>();
        header.put(CommandUtils.COMMAND_HEADER_TO,userName);
        messageCommand.setHeaders(header);
        MyPayload payload = new MyPayload();
        payload.setMsg(getUserOption());
        messageCommand.setPayload(payload);
        nettyClient.send(host, port, messageCommand);

    }



    public String getUserOption() {
        System.out.println("请输入指令执行操作：");
        System.out.println("1.消息\n2.登出\n3.登录\n0.退出");
        Scanner clientInput = new Scanner(System.in);
        String nextLine = clientInput.nextLine();
        return nextLine;
    }
}
